﻿function Maze(height, width) {
    this.height = height;
    this.width = width;
    this.matrix = new Array(this.height);
    for (var i = 0; i < this.height; i++) {
        this.matrix[i] = new Array(this.width);
        for (var j = 0; j < this.width; j++) {
            this.matrix[i][j] = $('<td></td>').text("#");
        }
    }

//    this.matrix[0][2] = $('<td></td>').text("#");
//    this.matrix[0][8] = $('<td></td>').text("#");
//    this.matrix[1][1] = $('<td></td>').text("#");
//    this.matrix[1][3] = $('<td></td>').text("#");
//    this.matrix[1][4] = $('<td></td>').text("#");
//    this.matrix[1][6] = $('<td></td>').text("#");
//    this.matrix[1][8] = $('<td></td>').text("#");
//    this.matrix[2][5] = $('<td></td>').text("#");
//    this.matrix[3][0] = $('<td></td>').text("#");
//    this.matrix[3][6] = $('<td></td>').text("#");
//    this.matrix[4][4] = $('<td></td>').text("#");
//    this.matrix[4][8] = $('<td></td>').text("#");
//    this.matrix[6][9] = $('<td></td>').text("#");
//    this.matrix[7][1] = $('<td></td>').text("#");
//    this.matrix[8][1] = $('<td></td>').text("#");
//    this.matrix[8][3] = $('<td></td>').text("#");
//    this.matrix[8][8] = $('<td></td>').text("#");
//    this.matrix[8][9] = $('<td></td>').text("#");
//    this.matrix[9][2] = $('<td></td>').text("#");
//    this.matrix[9][3] = $('<td></td>').text("#");
//    this.matrix[9][4] = $('<td></td>').text("#");
}

Maze.prototype.generate = function () {
    var table = $('<table></table>').addClass('table');

    for (var i = 0; i < this.height; i++) {
        var row = $('<tr></tr>').addClass('tr');
        row.append(this.matrix[i]);
        table.append(row);
    }

    return table;
}

Maze.prototype.create = function (x, y) {
    this.matrix[x][y] = $('<td></td>').text("");

    while (x + y < 18) {
        var dir = Math.floor(Math.random() * 4);
        switch (dir) {
            case 0: if (!this.outsideMaze(x, y - 1)) {
                        y = y - 1;
                        this.matrix[x][y] = $('<td></td>').text("");
                    }
                break;
            case 1: if (!this.outsideMaze(x + 1, y)) {
                        x = x + 1;
                        this.matrix[x][y] = $('<td></td>').text("");
                    }
                break;
            case 2: if (!this.outsideMaze(x, y + 1)) {
                        y = y + 1;
                        this.matrix[x][y] = $('<td></td>').text("");
                    }
                break;
            case 3: if (!this.outsideMaze(x - 1, y)) {
                        x = x - 1;
                        this.matrix[x][y] = $('<td></td>').text("");
                    }
                break;
        }
    }
    this.matrix[x][y] = $('<td></td>').text("");
}

Maze.prototype.findPath = function (x, y) {
    if (this.outsideMaze(x, y))
        return false;
    if (this.isGoal(x, y)) {
        this.matrix[x][y] = $('<td></td>').text("~");
        return true;
    }
    if (this.notOpen(x, y))
        return false;

    if (this.matrix[x][y].html() == "~")
        return false;
    this.matrix[x][y] = $('<td></td>').text("~");

    if (this.findPath(x, y - 1))
        return true;
    if (this.findPath(x + 1, y))
        return true;
    if (this.findPath(x, y + 1))
        return true;
    if (this.findPath(x - 1, y))
        return true;

    this.matrix[x][y] = $('<td></td>').text("*");

    return false;
}

Maze.prototype.outsideMaze = function(x, y) {
    if (x < 0 || x > 9)
        return true;
    else if (y < 0 || y > 9)
        return true;
    else
        return false;
}

Maze.prototype.isGoal = function(x, y) {
    if (x == 9 && y == 9)
        return true;
    else
        return false;
}

Maze.prototype.notOpen = function(x, y) {
    if (this.matrix[x][y].html() == "#")
        return true;
    else
        return false;
}
